package com.hunan.api.catv.service.biz.iptv;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.impl.iptv.IptvService;
import com.hunan.api.catv.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Service
public class IptvBiz extends ResultService {

    @Autowired
    IptvService iptvService;

    @Autowired
    CacheBiz cacheBiz;

    public JsonArray queryTelecastList(Integer count, Integer pageNo, String st, String et) {
        JsonObject iptvConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.iptv_conf), JsonObject.class);
        String domain = iptvConf.get("domainCms").getAsString();
        String appid = iptvConf.get("appid").getAsString();
        String secret = iptvConf.get("secretCms").getAsString();
        Map<String, String> body = new HashMap<String, String>();
        String mt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        body.put("mt", mt);
        body.put("appid", appid);
        body.put("count", count.toString());
        body.put("pageNo", pageNo.toString());
        if (!ObjectUtils.isEmpty(st)) {
            body.put("st", st);
        }
        if (!ObjectUtils.isEmpty(et)) {
            body.put("et", et);
        }
        JsonObject result = iptvService.queryTelecastList(domain, secret, body);
        if (!ObjectUtils.isEmpty(result)) {
            String code = result.get("code").getAsString();
            if ("0".equals(code)) {
                return result.get("data").getAsJsonArray();
            }
        }
        return null;
    }

    public JsonObject queryScheduleList(Integer count, Integer pageNo, String channelIds, String st, String et) {
        JsonObject iptvConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.iptv_conf), JsonObject.class);
        String domain = iptvConf.get("domainCms").getAsString();
        String appid = iptvConf.get("appid").getAsString();
        String secret = iptvConf.get("secretCms").getAsString();
        Map<String, String> body = new HashMap<String, String>();
        String mt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        body.put("mt", mt);
        body.put("appid", appid);
        body.put("pageNo", pageNo.toString());
        body.put("count", count.toString());
        body.put("channelIds", channelIds);
        if (!ObjectUtils.isEmpty(st)) {
            body.put("st", st);
        }
        if (!ObjectUtils.isEmpty(et)) {
            body.put("et", et);
        }
        JsonObject result = iptvService.queryScheduleList(domain, secret, body);
        if (!ObjectUtils.isEmpty(result)) {
            String code = result.get("code").getAsString();
            if ("0".equals(code)) {
                return result;
            }
        }
        return null;
    }

    public JsonObject getPlayAddressByIdWNApp(String userId, String userIp, String userMac, String videoId, Integer videoType, String beginTime, String endTime) {
        JsonObject iptvConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.iptv_conf), JsonObject.class);
        String domain = iptvConf.get("domainEpg").getAsString();
        String platform = iptvConf.get("appid").getAsString();
        String secretKey = iptvConf.get("secretEpg").getAsString();
        Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("version", "1.0");
        uriVariables.put("userId", userId);
        uriVariables.put("userMac", userMac);
        uriVariables.put("userIp", userIp);
        String authTimestamp = String.valueOf(System.currentTimeMillis());
        String signStr = userId + authTimestamp + secretKey;
        uriVariables.put("authTimestamp", authTimestamp);
        uriVariables.put("platform", platform);
        uriVariables.put("videoId", videoId);
        uriVariables.put("videoType", videoType.toString());
        uriVariables.put("beginTime", beginTime);
        uriVariables.put("endTime", endTime);
        JsonObject result = iptvService.getPlayAddressByIdWNApp(domain, signStr, secretKey, uriVariables);
        if (!ObjectUtils.isEmpty(result)) {
            String code = result.get("result").getAsJsonObject().get("state").getAsString();
            if ("0".equals(code)) {
                return result;
            }
        }
        return null;
    }

    public JsonObject vodChangeNotifyV2(JsonObject body) {

        return body;
    }
}
